<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Request;
use \think\facade\Session;
use think\facade\Config;

/*后台基础类 后台所有类文件继承此文件 检测session信息 共用信息类*/
class Base extends Controller{

   /*所有操作及页面进入前进行session检测*/
	public function check_session(){
        if(Session::has('username')!=1){
        	$this->error('请先登录','Index/login');
        	return false;
        }
	}

    /*所有页面需进行导航的选中信息*/
	public function navleft($data_get){
        $data=explode('-',$data_get);
        $result=['first'=> $data[0],'second' => $data[1]];
        return $result;
	}

    /*上传接口*/
    public function upload(Request $request){
        $get=$request->get();
        $upload_url=$get['upload_url'];
        $allowedExts = Config::get('app.allowedExts');//允许上传的图片后缀
        $size = Config::get('app.upload_file_size');//允许上传的图片大小
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);// 获取文件后缀名
        if (($_FILES["file"]["size"] < $size) && in_array($extension, $allowedExts)){

            if ($_FILES["file"]["error"] > 0){
                $result=['status'=>'0','msg'=>"'".$_FILES["file"]["error"]."'"];
                return json($result);
            }else{
                /*移动到上传目录*/
                $newname=file_name().'.'.$extension;
                move_uploaded_file($_FILES["file"]["tmp_name"],$upload_url.$newname);
                if($extension=='mp4'){ $type=2;}else{ $type=1;}
                $result=['status'=>'1','msg'=>'上传成功','newname'=>'/'.$upload_url.$newname,'type'=>$type];
                return json($result);
            }
        }else{
            $result=['status'=>'0','msg'=>'非法的文件格式'];
            return json($result);
        }

    }

    /*kindeditor上传接口*/
    public function kindeditor_upload(Request $request){
        $get=$request->get();
        $upload_url='static/upload/kindeditor/'.$get['dir']."/";

        $allowedExts = Config::get('app.allowedExts');//允许上传的图片后缀
        $size = Config::get('app.upload_file_size');//允许上传的图片大小
        $temp = explode(".", $_FILES["imgFile"]["name"]);
        $extension = end($temp);// 获取文件后缀名
        if (($_FILES["imgFile"]["size"] < $size) && in_array($extension, $allowedExts)){

            if ($_FILES["imgFile"]["error"] > 0){
                $result=['error'=>1,'message'=>"'".$_FILES["imgFile"]["error"]."'"];
                return json($result);
            }else{
                /*移动到上传目录*/
                $newname=file_name().'.'.$extension;
                move_uploaded_file($_FILES["imgFile"]["tmp_name"],$upload_url.$newname);
                $result=['error'=>0,'url'=>'/'.$upload_url.$newname];
                return json($result);
            }
        }else{
            $result=['error'=>1,'message'=>'非法的文件格式'];
            return json($result);
        }

    } 

    public function kindeditor_upload_class(Request $request){
        $get=$request->get();
        $upload_url='static/upload/class/';

        $allowedExts = Config::get('app.allowedExts');//允许上传的图片后缀
        $size = Config::get('app.upload_file_size');//允许上传的图片大小
        $temp = explode(".", $_FILES["imgFile"]["name"]);
        $extension = end($temp);// 获取文件后缀名
        if (($_FILES["imgFile"]["size"] < $size) && in_array($extension, $allowedExts)){

            if ($_FILES["imgFile"]["error"] > 0){
                $result=['error'=>1,'message'=>"'".$_FILES["imgFile"]["error"]."'"];
                return json($result);
            }else{
                /*移动到上传目录*/
                $newname=file_name().'.'.$extension;
                move_uploaded_file($_FILES["imgFile"]["tmp_name"],$upload_url.$newname);
                $result=['error'=>0,'url'=>'/'.$upload_url.$newname];
                return json($result);
            }
        }else{
            $result=['error'=>1,'message'=>'非法的文件格式'];
            return json($result);
        }

    } 
   
       
}